package com.wmx.mp.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wmx.mp.entity.Emp;
import com.wmx.mp.service.IEmpService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 汪茂雄
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Resource
    private IEmpService empService;

    /**
     * http://localhost:8080/emp/pageList?page=1&count=10
     *
     * @param page  ：查询页码，从1开始
     * @param count ：每页展示的条数
     * @return
     */
    @GetMapping("/pageList")
    public IPage<Emp> pageList(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer count) {
        Page<Emp> pageDTO = PageDTO.of(page, count);
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        Page<Emp> deptPage = empService.page(pageDTO, queryWrapper);
        return deptPage;
    }

    /**
     * http://localhost:8080/emp/saveList
     * 插入操作时：如果属性没有指定，或者指定值为null，则属性名称不会出现在 insert into sql中。
     * <pre>
     *     [
     *     {
     *         "comm": 9000,
     *         "dept_No": 2,
     *         "hireDate": "1991-05-05T00:00:00",
     *         "job": "明教教主",
     *         "name": "张无忌",
     *         "sal": 8000.88
     *     },
     *     {
     *         "comm": 6000.55,
     *         "deptNo": 3,
     *         "hireDate": "1993-01-05T00:00:00",
     *         "job": "元廷郡主",
     *         "name": "赵敏",
     *         "sal": 8500.8
     *     }
     * ]
     * </pre>
     *
     * @param empList
     * @return
     */
    @PostMapping("/saveList")
    public Map<String, String> saveList(@RequestBody List<Emp> empList) {
        /**
         * <pre>
         * ==> Preparing: INSERT INTO EMP ( emp_no, NAME, JOB, HIRE_DATE, SAL, DEPT_NO, CREATE_TIME, UPDATE_TIME ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
         * ==> Parameters: 1535119363(Integer), 赵敏(String), 元廷郡主(String), 1993-01-05T00:00(LocalDateTime), 8500.8(BigDecimal), 3(Integer), 2023-04-01T15:30:44.165(LocalDateTime), 2023-04-01T15:30:44.165(LocalDateTime)
         * </pre>
         */
        empService.saveOrUpdateBatch(empList);
        Map<String, String> resultMap = MapUtil.builder("code", "200").put("msg", "操作成功").build();
        return resultMap;
    }

}
